package com.bot.nft.module.cm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.bot.nft.core.enums.cm.Enum;
import com.bot.nft.module.cm.domain.CmCarouselInfo;

import com.bot.nft.module.cm.domain.bo.CmCarouselInfoBo;
import com.bot.nft.module.cm.domain.vo.CmCarouselInfoVo;
import com.bot.nft.module.cm.mapper.ICmCarouselMapper;
import com.bot.nft.module.cm.service.ICmCarouselService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 描述: 首页轮播图业务接口实现类
 * <p>
 * 时间: 2023-11-28 20:28
 * 负责人: 卢梦丽
 **/
@Service
public class CmCarouselServiceImpl extends ServiceImpl<ICmCarouselMapper, CmCarouselInfo> implements ICmCarouselService {

    @Resource
    private ICmCarouselMapper iCarouselMapper;

    /**
     * 新增首页轮播图功能
     *
     * @param carouselCmInfoBo 首页轮播图信息，包含首页轮播图，轮播图序号等
     * @return Boolean 成功/true，失败/false
     */
    @Override
    public Boolean addCarouselCmInfo(CmCarouselInfoBo carouselCmInfoBo) {
        carouselCmInfoBo.setId(IdUtil.getSnowflakeNextIdStr());
        carouselCmInfoBo.setCreateTime(new Date());
        CmCarouselInfo cmCarouselInfo = BeanUtil.toBean(carouselCmInfoBo, CmCarouselInfo.class);
        cmCarouselInfo.setDeleteFlag(0);
        cmCarouselInfo.setCarouselNum(999999999);
        int insert = iCarouselMapper.insert(cmCarouselInfo);
        Boolean msg = false;
        if (insert==1){
            msg = true;
        }

        return msg;
    }

    /**
     * 修改轮播内容功能
     *
     * @param carouselCmInfoBo 首页轮播图信息，包含首页轮播图，轮播图序号等
     * @return Boolean 成功/true，失败/false
     */
    @Override
    public Boolean updateCarouselCmInfo(CmCarouselInfoBo carouselCmInfoBo) {
        CmCarouselInfo cmCarouselInfo = BeanUtil.toBean(carouselCmInfoBo, CmCarouselInfo.class);//业务模型转实体

        cmCarouselInfo.setUpdateTime(new Date());
        int i = iCarouselMapper.updateById(cmCarouselInfo);//调用mapper接口
        Boolean msg = false;
        if (i==1){
            msg = true;
        }

        return msg;

    }

    /**
     * 查看轮播内容功能
     *
     * @param id 首页轮播图信息标识
     * @return 首页轮播图信息，包含首页轮播图，轮播图序号等
     */
    @Override
    public CmCarouselInfoVo getCarouselCmInfo(String id) {
        CmCarouselInfoVo cmCarouselInfoVo = iCarouselMapper.selectVoById(id);

        return cmCarouselInfoVo;

    }

    /**
     * 删除首页轮播图功能
     *
     * @param id 首页轮播图信息标识
     * @return Boolean 成功/true，失败/false
     */
    @Override
    public Boolean delCarouselCmInfo(String id) {
        boolean update = this.lambdaUpdate()
                .set(CmCarouselInfo::getDeleteFlag,Enum.ALDEL.getValue())
                .eq(CmCarouselInfo::getId, id)
                .update();


        return update;

    }

    /**
     * 分页查询轮播内容功能
     *
     * @param keyword  首页轮播图关键字
     * @param pageNo   当前页数
     * @param pageSize 每页显示条数
     * @return 首页轮播图信息*
     */
    @Override
    public Page<CmCarouselInfoVo> queryCarouselByPage(String keyword, Integer status, Integer pageNo, Integer pageSize) {
     //开启分页
        Page<CmCarouselInfo> page = new Page<>(pageNo, pageSize);
        QueryWrapper<CmCarouselInfo> queryWrapper = new QueryWrapper<>();
        //判断关键字不为空
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.lambda().like(CmCarouselInfo::getTitle, keyword);
        }
        //判断状态不为空
        if (null != status) {
            queryWrapper.lambda().like(CmCarouselInfo::getStatus, status);
        }
        queryWrapper.lambda().like(CmCarouselInfo::getDeleteFlag,Enum.NODEL.getValue());
        queryWrapper.lambda().orderByAsc(CmCarouselInfo::getCarouselNum);
        //查询结果
        Page<CmCarouselInfoVo> dcAuthorInfoPage = iCarouselMapper.selectVoPage(page, queryWrapper);
        return dcAuthorInfoPage;

    }

    /**
     * 调整轮播内容顺序功能(升序)
     *
     * @param id 首页轮播图标识
     * @return Boolean 成功/true，失败/false
     */
    @Override
    public Boolean adjustCarouselCmInfo(String id, Integer carouselNum) {
        //上一条序列号
        Integer upCarouselNum = carouselNum - 1;

        boolean adjust = false;
        Boolean upAdjustCarouselCmInfo = iCarouselMapper.upAdjustCarouselCmInfo(upCarouselNum, carouselNum);//根据上一条数据的序列号，把上一条数据的序列号下移
        Boolean adjustCarouselCmInfo = iCarouselMapper.adjustCarouselCmInfo(id, upCarouselNum);//再根据点击当前事件的id，把序列号改为上一条序列号
        if (upAdjustCarouselCmInfo && adjustCarouselCmInfo) {
            adjust = adjustCarouselCmInfo;
        }
        return adjust;
    }


    /**
     * 调整轮播内容顺序功能(降序)
     *
     * @param id
     * @param carouselNum
     * @return
     */
    @Override
    public Boolean descAdjustCarouselCmInfo(String id, Integer carouselNum) {
        //下一条序列号
        Integer upCarouselNum = carouselNum + 1;

        boolean adjust = false;
        Boolean upAdjustCarouselCmInfo = iCarouselMapper.upAdjustCarouselCmInfo(upCarouselNum, carouselNum);
        Boolean adjustCarouselCmInfo = iCarouselMapper.adjustCarouselCmInfo(id, upCarouselNum);
        if (upAdjustCarouselCmInfo && adjustCarouselCmInfo) {
            adjust = adjustCarouselCmInfo;
        }
        return adjust;


    }

    /**
     * 禁用轮播内容功能
     *
     * @param id 首页轮播图标识
     * @return Boolean 成功/true，失败/false
     */
    @Override
    public Boolean disCarouselCmInfo(String id) {

        boolean update = this.lambdaUpdate()
                .set(CmCarouselInfo::getStatus, Enum.END.getValue())
                .set(CmCarouselInfo::getCarouselNum,Enum.CAROUSELNUM.getValue())
                .eq(CmCarouselInfo::getId, id)
                .update();
        return update;
    }

    /**
     * 启用轮播内容功能
     *
     * @param id 首页轮播图标识
     * @return Boolean 成功/true，失败/false
     */
    @Override
    public Boolean startCarouselCmInfo(String id, Integer carouselNum) {
        Integer total = iCarouselMapper.totalCarouselCmInfo();
        if (total == 0) {
            carouselNum = 0;
        } else {
            carouselNum = (total + 1);
        }
        boolean update = this.lambdaUpdate()
                .set(CmCarouselInfo::getStatus, Enum.START.getValue())
                .set(CmCarouselInfo::getCarouselNum,carouselNum)
                .eq(CmCarouselInfo::getId, id)
                .update();
        return update;
    }

    /**
     * 预览轮播内容功能
     *
     * @param id 首页轮播图标识
     * @return 首页轮播图信息，包含首页轮播图，轮播图序号等
     */
    @Override
    public CmCarouselInfoVo preCarouselCmInfo(String id) {
        CmCarouselInfoVo cmCarouselInfoVo = iCarouselMapper.selectVoById(id);
        return cmCarouselInfoVo;
    }


    /**
     * 默认查询已启用的轮播图 (app端)
     * @return
     */
    @Override
    public List<CmCarouselInfoVo> queryCarousel() {
        QueryWrapper<CmCarouselInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CmCarouselInfo::getStatus,Enum.START.getValue());
        List<CmCarouselInfoVo> cmCarouselInfoVos = iCarouselMapper.selectVoList(queryWrapper);
        return cmCarouselInfoVos;
    }


}
